#!/bin/bash

TmpDirectory="/Users/zhxw/work/tmp/"
PayloadPath="${TmpDirectory}Payload"
PlistBuddy="/usr/libexec/PlistBuddy"
OriginFile="$1"
ResignedFile="${OriginFile%.*}-resigned.ipa"
ResignProvisionFile="/Users/zhxw/work/ResignAppFiles/iRegign.mobileprovision"
CertificationName="iPhone Distribution: HA Youmi INC"
# ResignProvisionFile="/Users/zhxw/work/dis0327tgbh.mobileprovision"
# CertificationName="iPhone Distribution: Anne Stone (B333B64942)"

# 后面添加的，批量打包的时候可以统一保存到一个地方，免得混在一起不好区分
saveDir=$2
if [ -z $saveDir ]; then
    ResignedFile="${OriginFile%.*}-resigned.ipa"
else
    # path=${OriginFile%/*}
    path=`dirname $OriginFile`
    cpath="${path}/${saveDir}"
    if [ ! -d $cpath ]; then
        mkdir $cpath
    fi
    # fileNameE=${OriginFile##*/}
    # name=${fileNameE%.*}
    name=`basename $OriginFile .ipa`
    ResignedFile="${cpath}/${name}-resigned.ipa"
fi


if [ -f "$ResignedFile" ]; then
    echo "find old resigned file, will delete it!"
    rm -rf "$ResignedFile"
fi

if [ -d $TmpDirectory ]; then 
    echo "find tmp directory, will delete it!"
    rm -rf $TmpDirectory
fi

#解压ipa包, 生成Payload文件夹
unzip -qo "$1" -d $TmpDirectory

appPath=`ls $PayloadPath`
resignAppPath="${PayloadPath}/${appPath}"
#Info.plist文件路径
infoPath="${resignAppPath}/Info.plist"
codeSignaturePath="${resignAppPath}/_CodeSignature"
#删除_CodeSignature文件夹
rm -rf $codeSignaturePath

embeddedProvisionFile="${resignAppPath}/embedded.mobileprovision"

#替换embedded.mobileprovision
cp "$ResignProvisionFile" "$embeddedProvisionFile"

#**********************需要修改application-identifier为新的BundleId**************#
fullEntitlesFile="${TmpDirectory}entitlements_full.plist"
targetEntitleFile="${TmpDirectory}entitlements.plist"

security cms -D -i "$embeddedProvisionFile" > $fullEntitlesFile
$PlistBuddy -x -c 'Print:Entitlements' $fullEntitlesFile > $targetEntitleFile

fileName=${appPath%.*}

#************************8拼接新的BundleId, 用在entitlements.plist文件中********************#
###注意:(花一整天总结出来的血泪教训)
###如果同一个包打包时, 前一次application-identifier设置的是通配符(*), 那后一次这里设置了application-identifier的话, 安装会失败!!!!
###反之亦然
#获取要重打包应用的BundleId
bundleId=$($PlistBuddy -c "Print :CFBundleIdentifier" "$infoPath")
teamId=$($PlistBuddy -c "Print :com.apple.developer.team-identifier" "$targetEntitleFile")
targetBundleId="$teamId.$bundleId"
$PlistBuddy -c "Set :application-identifier $targetBundleId" "$targetEntitleFile"
$PlistBuddy -c "Delete :com.apple.developer.team-identifier" "$targetEntitleFile"
$PlistBuddy -c "Delete :keychain-access-groups" "$targetEntitleFile"

find -d "$TmpDirectory"  \( -name "*.app" -o -name "*.appex" -o -name "*.framework" -o -name "*.dylib" \) > "${TmpDirectory}needCodeSigns.txt"

while IFS='' read -r line || [[ -n "$line" ]]; do
    codesign --continue -f -s "$CertificationName" --entitlements="$targetEntitleFile" "$line"
done < "${TmpDirectory}needCodeSigns.txt"

#压缩Payload并改后缀为ipa
cd "$TmpDirectory"
zip -qry "$ResignedFile" "Payload"
# mv ab.ipa "$ResignedFile"

echo "output file: $ResignedFile"
#扫尾
rm -rf $TmpDirectory

# ideviceinstaller -i "$ResignedFile"
